/**
 * Created by Administrator on 2015/10/23.
 */
define(function (require, exports, module) {
    var folderLists = {};
    var CMISService = {};
    CMISService.getFolderTree = function(rootFolderId){
        return $.when(window.rootFolder);
    };

    CMISService.moveTo = function(doc, sourceFolderId, targetFolderId){
        var dtd = $.Deferred();      
        $.ajax({
            type: "GET",
            url: "/dev/move-document",
            data: {
                docId: doc.id,
                sourceFolderId: sourceFolderId,
                targetFolderId: targetFolderId
            },
            async: true,
            dataType: "json"})
            .success(function (response) {
                if (response.errorCode === 0){
                    _removeDocument(folderLists[sourceFolderId], doc);         
                    delete folderLists[targetFolderId]; //需要更新
                    dtd.resolve(response);
                }
                else{
                    dtd.resolve(response);
                }
            })
            .error(function (e) {
                alert(e);
                dtd.reject();
            });
        return dtd.promise();
    };

    CMISService.copyTo = function(doc, folderId){
        var dtd = $.Deferred();
        $.ajax({
            type: "GET",
            url: "/dev/copy-document",
            data: {
                docId : doc.id,
                folderId: folderId
            },
            async: true,
            dataType: "json"})
            .success(function (response) {
                if (response.errorCode === 0){
                    delete folderLists[folderId]; //需要更新
                    dtd.resolve(response);
                }
                else{
                    dtd.reject(response);
                }               
            })
            .error(function (e) {
                alert(e);
                dtd.reject(e);
            });
        return dtd.promise();
    };
                
    CMISService.getFolderChildren = function(folderId){
        if (folderLists[folderId]){
            return $.when(folderLists[folderId]);
        }
        var dtd = $.Deferred();
        $.ajax({
            type: "GET",
            url: "/dev/get-folder-children",
            data: {
                folderId: folderId
            },           
            dataType: "json",
            success: function (data) {
                if (data.data) {
                    data.data.sort(function(a, b) {
                        if (a.type === 'cmis:folder' && a.type !== b.type) {
                            return -1;
                        }
                        if (b.type === 'cmis:folder' && a.type !== b.type) {
                            return 1;
                        }
                        return 0;
                    });
                }
                folderLists[folderId] = data;
                dtd.resolve(data);                
            },
            error: function (e) {              
                delete folderLists[folderId];  //delete promise
                dtd.reject({message:e.responseText});        
            }
        });
        var promise = dtd.promise();
        folderLists[folderId] = promise;
        return promise;
    };

    function _removeDocument(folder, doc){
        folder.data = _.without(folder.data, doc);
        folder.count -= 1;
        folder.docNum -= 1;
    }
    CMISService.removeDocument = function(doc, folderId) {
        var dtd = $.Deferred();
        $.ajax({
            type: "GET",
            url: "/dev/remove-document",
            data: {
                docId: doc.id,
                sourceFolderId: folderId
            },
            async: true,
            dataType: "json",
            success: function (response) {
                if (response.errorCode === 0) {    
                    _removeDocument(folderLists[folderId], doc);         
                    dtd.resolve(response);
                }
                else{
                    dtd.reject(response);      
                }              
            },
            error: function (e) {
                dtd.reject({message:e.responseText});        
            }
        });
        return dtd.promise();
    };

    CMISService.removeFolder = function(folder) {    
        var dtd = $.Deferred();    
        $.ajax({
            type: "GET",
            url: "/dev/remove-folder",
            data: {
                folderId: folder.id             
            },          
            dataType: "json",
            success: function (response) {
                if (response.errorCode === 0) {
                    removeFolderInChildren(window.rootFolder, folder);
                    $(window.rootFolder).trigger("change");
                    dtd.resolve(response);
                }
                else{
                    dtd.reject(response);      
                }
            },
            error: function (e) {                                  
                dtd.reject({message:e.responseText});        
            }
        });
        return dtd.promise();     
    };

    function removeFolderInChildren(parentFolder, folder){
        if (parentFolder.children) {
            _.each(parentFolder.children, function(node){
                if (node === folder){
                    parentFolder.children = _.without(parentFolder.children, folder);
                }
                else{
                    removeFolderInChildren(node, folder);
                }              
            });
        }
    }

    CMISService.doSearch = function(keyword, option) {
        var dtd = $.Deferred();         
         $.ajax({
            type: "POST",
            url: "/search/search",
            data: _.extend({keyword : keyword}, option),
            async: true,
            dataType: "json",
            success: function (response) {
                dtd.resolve(response);
            },
            error: function (e) {
                 dtd.reject({message:e.responseText});
            }
        });
        return dtd.promise();      
    };

    CMISService.getAcl = function(folderId){
        var dtd = $.Deferred();
        $.ajax({
            type: "GET",
            url: "/dev/get-acl",
            data: {
                folderId: folderId
            },
            dataType: "json",
            success: function (data) {
                dtd.resolve(data);
            },
            error: function (e) {
                alert(e);
                dtd.reject();
            }
        });
        return dtd.promise();
    };

    CMISService.getPermissionList = function(){
        var dtd = $.Deferred();
        $.ajax({
            type: "GET",
            url: "/dev/get-permission-list",
            data: {
            },
            dataType: "json",
            success: function (data) {
                dtd.resolve(data);
            },
            error: function (e) {
                alert(e);
                dtd.reject();
            }
        });
        return dtd.promise();
    };

    CMISService.filingDocument = function(document){
        var dtd = $.Deferred();
        $.ajax({
            type: "POST",
            url: "/archive/filing-document",
            data: {
                'document' : JSON.stringify(document)
            },
            dataType: "json",
            success: function (response) {              
                if (response.errorCode === 0){                  
                    dtd.resolve(response);
                }
                else{
                    dtd.reject(response);
                }     
            },
            error: function (e) {                        
                dtd.reject({message:e.responseText});
            }
        });
        return dtd.promise();
    };

    window.CMISService = CMISService;
    return CMISService;
});
